import {
  Column,
  CreateDateColumn,
  DeleteDateColumn,
  Entity,
  JoinColumn,
  ManyToOne,
  PrimaryGeneratedColumn,
  Tree,
  TreeChildren,
  TreeParent,
  UpdateDateColumn,
} from 'typeorm';
import { User } from '@/modules/user/entities/user.entity';
import { Article } from '@/modules/article/entities/article.entity';

export enum CommentStatus {
  // 正常
  NORMAL = 'normal',
  // 锁定
  LOCKED = 'locked',
}

@Entity()
@Tree('closure-table', {
  closureTableName: 'comment_closure_table',
  ancestorColumnName: (column) => 'ancestor_' + column.propertyName,
  descendantColumnName: (column) => 'descendant_' + column.propertyName,
})
export class Comment {
  @PrimaryGeneratedColumn()
  commentId: number;

  @Column({ type: 'varchar', length: 255, comment: '评论内容' })
  content: string;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'userId' })
  user: User;

  @ManyToOne(() => User, { nullable: true })
  @JoinColumn({ name: 'replyUserId' })
  replyUser: User;

  @ManyToOne(() => Article)
  @JoinColumn({ name: 'articleId' })
  article: Article;

  @TreeParent()
  parent: Comment;

  @TreeChildren()
  children: Comment[];

  @Column({ type: 'enum', enum: CommentStatus, default: CommentStatus.NORMAL, comment: '评论状态' })
  status: CommentStatus;

  @CreateDateColumn({ type: 'timestamp', comment: '创建日期' })
  createDate: Date;

  @UpdateDateColumn({ type: 'timestamp', comment: '更新日期' })
  updateDate: Date;

  @DeleteDateColumn({ type: 'timestamp', select: false, comment: '删除日期' })
  removeDate: Date;
}
